package com.sansi.bleFloodlight.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.flyco.tablayout.SlidingTabLayout;

import com.my.httpapi.api.annotation.Layout;
import com.my.httpapi.api.baseView.BaseActivity;
import com.sansi.bleFloodlight.R;
import com.sansi.bleFloodlight.fragment.HomeGroupFragment;
import com.sansi.bleFloodlight.fragment.HomeLightFragment;
import com.sansi.bleFloodlight.utils.ActivityUtils;
import com.sansi.bleFloodlight.utils.RouterPath;
import com.sansi.bleFloodlight.view.TriangleDrawable;
import com.zyyoona7.popup.EasyPopup;
import com.zyyoona7.popup.XGravity;
import com.zyyoona7.popup.YGravity;


import java.util.ArrayList;

@Layout(R.layout.layout_activity_tab)
@Route(path = RouterPath.tabPath)
public class MainActivity extends BaseActivity {

    private SlidingTabLayout slidingTabLayout;
    private ImageView ivAdd, ivAppinfo;
    private ViewPager viewPager;
    private MyPagerAdapter mAdapter;
    private ArrayList<Fragment> mFragments = new ArrayList<>();
    private final String[] mTitlt = new String[2];
    private EasyPopup easyPopup;

    @Override
    protected void initView() {
        slidingTabLayout = findViewById(R.id.slidingTabLayout);
        viewPager = findViewById(R.id.vp_container);
        ivAdd = findViewById(R.id.iv_add);
        ivAppinfo = findViewById(R.id.iv_appinfo);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        mTitlt[0] = getString(R.string.tab_light);
        mTitlt[1] = getString(R.string.tab_group);
        initEasyPopup();
        mFragments.add(HomeLightFragment.newInstance());
        mFragments.add(HomeGroupFragment.newInstance());
        mAdapter = new MyPagerAdapter(getSupportFragmentManager());
        viewPager.setAdapter(mAdapter);
        slidingTabLayout.setViewPager(viewPager);

        ivAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showEasyPopup(view);
            }
        });

        ivAppinfo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                RouterPath.gotoAppInfoPath();
            }
        });
    }

    @Override
    protected void destory() {

    }

    @Override
    public void onBackPressed() {
        if (ActivityUtils.isExit()) {
            super.onBackPressed();
            return;
        }
    }

    @Override
    protected String showTitle() {
        return null;
    }

    @Override
    protected boolean statusBarIsBlack() {
        return false;
    }

    @Override
    protected boolean showLeftArrow() {
        return false;
    }

    private void initEasyPopup() {
        easyPopup = EasyPopup.create()
                .setContext(this)
                .setContentView(R.layout.layout_pop_home)
                .setAnimationStyle(R.style.RightTop2PopAnim)
                .setOnViewListener(new EasyPopup.OnViewListener() {
                    @Override
                    public void initViews(View view, EasyPopup popup) {
                        View arrowView = view.findViewById(R.id.v_arrow);
                        TextView tvAddDevice = view.findViewById(R.id.tv_add_device);
                        TextView tvAddGroup = view.findViewById(R.id.tv_add_group);
                        arrowView.setBackground(new TriangleDrawable(TriangleDrawable.TOP, getResources().getColor(R.color.color_1a1a1a_5)));

                        tvAddDevice.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                easyPopup.dismiss();
                                RouterPath.gotoBleSearchPath();
                            }
                        });
                        tvAddGroup.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                easyPopup.dismiss();
                                RouterPath.gotoAddGroupPath();
                            }
                        });
                    }
                })
                .setFocusAndOutsideEnable(true)
                .apply();
    }

    private void showEasyPopup(View view) {
        easyPopup.showAtAnchorView(view, YGravity.BELOW, XGravity.ALIGN_RIGHT, 0, 0);
    }

    private class MyPagerAdapter extends FragmentPagerAdapter {
        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public int getCount() {
            return mFragments.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mTitlt[position];
        }

        @Override
        public Fragment getItem(int position) {
            return mFragments.get(position);
        }
    }
}
